package com.dl.controller;

import com.dl.pojo.Page;
import com.dl.pojo.Student;
import com.dl.pojo.User;
import com.dl.service.StudentService;
import com.dl.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author DL
 * @create 2021-03-23 14:26
 */
@Api(tags="学生操作接口")
@Controller
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @Resource
    private UserService userService;
    private Page<Student> page;

    @ApiOperation("回到首页")
    @GetMapping("/goBack")
    public String goBack(){
        return "redirect:/student/info";
    }

    @ApiOperation("跳转学生首页")
    @GetMapping("/info")
    public String main(){
        return "student/student";
    }

    @ApiOperation("ajax分页查询学生，返回json。带简易缓存")
    @ResponseBody
    @GetMapping(value = {"/json","/json/{currentPage}"})
    public Page<Student> queryStudentByLimitJson(@PathVariable(required = false) Integer currentPage){
        //如果currentPage为null相当于第一次访问，进行处理。否则直接使用传入的页码
        if (currentPage == null) {
            //如果page.getCurrentPage()不为null，说明访问过页面。那么currentPage(当前页) = page.getCurrentPage() (存储的访问的页码)
            if (page != null && page.getCurrentPage() != null){
                currentPage = page.getCurrentPage();
            }else
                //传入页码为null并且未找到存储的页码，那么默认访问第一页
                currentPage = 1;
        }

        if (page == null) {
            page = studentService.queryStudentByLimit(currentPage, 10);
            page.setCurrentPage(currentPage);
            System.out.println("page为null的情况，第一次访问，进行查询。page对象里的页码：" + page.getCurrentPage()+ "，与传入的页码：" + currentPage);
            page.setMsg("");
            return page;
        }else{
            if (!page.getCurrentPage().equals(currentPage)) {
                System.out.println("page不为null的情况，不是第一次访问。但是page对象里的页码：" + page.getCurrentPage()+ "，与传入的页码：" + currentPage + "不同，说明访问了其他页面，进行查询");
                page = studentService.queryStudentByLimit(currentPage, 10);
                page.setCurrentPage(currentPage);
                page.setMsg("");
                return page;
            }
        }
        System.out.println("page不为null，但是page对象里的页码：" + page.getCurrentPage()+ "，与传入的页码：" + currentPage + "相同。说明访问的是相同页面，不进行查询，直接返回已有的page对象");
        page.setMsg("重复查询");
        return page;
    }

    @ApiOperation("关键字查询")
    @ResponseBody
    @GetMapping(value = {"/search/{keyWord}","/search/{keyWord}/{currentPage}"})
    public Page<Student> searchStudent(@PathVariable String keyWord, @PathVariable(required = false) Integer currentPage){
        //如果为空则是第一次访问，默认访问第一页
        if (currentPage == null) {
            currentPage = 1;
        }
        System.out.println("keyWord = " + keyWord);
        System.out.println("currentPage=>" + currentPage);
        Page<Student> page = studentService.queryStudentByKey(keyWord, currentPage, 10);
        //如果关键字没有查询到信息则返回null
        if (page != null) {
            page.setCurrentPage(currentPage);
            return page;
        }else {
            return null;
        }
    }

    @ApiOperation("跳转增加页面")
    @GetMapping("/toAdd")
    public String toAdd(){
        return "student/add";
    }

    @ApiOperation("添加学生")
    @ResponseBody
    @PostMapping("/add")
    public boolean add(@RequestBody Student student) {
        System.out.println("add=>" + student);
        boolean b1 = studentService.addStudent(student);
/*        boolean b2 = userService.addUser(new User(student.getName(), "123456"));
        if (b1 && b2){
            System.out.println("插入成功，数据的自增id值为：" + student.getUid());
        }*/
        return b1;
    }

    @ApiOperation("传入id删除学生")
    @ResponseBody
    @GetMapping("/del/{uid}")
    public boolean del(@PathVariable int uid) {
        System.out.println("del=>" + uid);
        page = null;//进行了修改删除等操作清空页面缓存数据
        return studentService.deleteStudent(uid);
    }

    @ApiOperation("跳转修改页面")
    @GetMapping("/update/{uid}")
    public String toUpdatePage(@PathVariable int uid, Model model){
        Student student = studentService.queryStudentById(uid);
        System.out.println(student);
        model.addAttribute("student",student);
        return "student/update";
    }

    @ApiOperation("修改学生")
    @ResponseBody
    @PostMapping("/update")
    public boolean update(@RequestBody Student student) {
        System.out.println("update=>" + student);
        page = null;//进行了修改删除等操作清空页面缓存数据
        return studentService.updateStudent(student);
    }

    /*    //查询全部学生
    @RequestMapping("/all")
    public String queryAllStudent(Model model){
        List<Student> students = studentService.queryAllStudent();
        model.addAttribute("students",students);
        return "main";
    }*/

/*    //普通分页查询学生，返回页面
    @RequestMapping(value = {"/main","/main/{currentPage}"})
    //@RequestParam(defaultValue = "1")
    public String queryStudentByLimit(Model model,@PathVariable(required = false) Integer currentPage){
        System.out.println("currentPage=>" + currentPage);
        if (currentPage == null) {
            currentPage = 1;
        }
        Page<Student> page = studentService.queryStudentByLimit(currentPage, 10);
        page.setCurrentPage(currentPage);
        //System.out.println("页面总数=>" + page.getTotalPage());

        model.addAttribute("page",page);
        return "main";
    }*/

}
